/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fyx.mula.web;

import java.io.InputStream;
import java.util.Collection;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.fyx.linker.web.LinkerLocal;
import org.fyx.mula.controll.MulaTranslateFacadeLocal;
import org.fyx.mula.ejb.IsoLanguage;
import org.fyx.mula.ejb.MulaLocal;
import org.fyx.mula.entity.MulaTranslate;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;

/**
 * Managed meana ke strance seznamu jazykovych mutaci.
 * Zoodpovida za spravnou editaci, vytvareni a mazani jazykovych mutaci
 * na vystupni vrstve.
 * 
 * @author ydenek
 */
@ManagedBean
@SessionScoped
public class MulaList {

    private StreamedContent file;
    @EJB
    private MulaLocal mula;
    @EJB
    private MulaTranslateFacadeLocal mulaTranslateFacade;
    @EJB
    private LinkerLocal linker;
    
    private Collection<MulaTranslate> mulaTranslates;

    /** Creates a new instance of MulaList */
    public MulaList() {
    }

    /**
     * Vrati soubor ke stazeni s exportovanymi preklady.
     * @return soubor ke stazeni
     */
    public StreamedContent getFile() {
        export();
        return file;
    }

    /**
     * Nastavi soubor ke stazeni s exportovanymi preklady.
     * @param file ke stazeni
     */
    public void setFile(StreamedContent file) {
        this.file = file;
    }

    public SelectItem[] getIsoLanguageOption() {
        String[] isoLanguageOption = new String[]{IsoLanguage.CZE, IsoLanguage.ENG};
        SelectItem[] options = new SelectItem[isoLanguageOption.length + 1];
        options[0] = new SelectItem("", "Select");
        for (int i = 0; i < isoLanguageOption.length; i++) {
            options[i + 1] = new SelectItem(isoLanguageOption[i], isoLanguageOption[i]);
        }

        return options;
    }

    public void save() {
        System.out.println("Ukladam");
        for (MulaTranslate mulaTranslate : mulaTranslates) {
            mulaTranslateFacade.edit(mulaTranslate);
        }
        linker.recreateMenuModel();
        FacesMessage msg = new FacesMessage(mula.get("Save sucessfull."));
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public Collection<MulaTranslate> getMulaTranslates() {
        mulaTranslates = mulaTranslateFacade.findAll();
        return mulaTranslates;
    }

    public void delete(MulaTranslate translate) {
        mulaTranslateFacade.remove(translate);
        mulaTranslates = mulaTranslateFacade.findAll();
    }

    public void export() {
        String buffer = "";
        for (MulaTranslate translate : mulaTranslates) {
            buffer += String.format("%s\t%s\t%s\n",
                    translate.getMulaTranslatePK().getKey(),
                    translate.getMulaTranslatePK().getIsoLanguage(),
                    translate.getTranslate());
        }
        InputStream is = new java.io.ByteArrayInputStream(buffer.getBytes());
        file = new DefaultStreamedContent(is, "text/plain", "mula.txt");
    }

    public void handleFileUpload(FileUploadEvent event) {
        UploadedFile ufile = event.getFile();
        String im = String.valueOf(ufile.getContents());
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, 
                mula.get("Import sucesfull."), im);
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}
